<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { remarkFormSchema } from './account.data';

  const emit = defineEmits(['success']);

  const title = ref('设置备注');
  const recordId = ref(''); // 用于存储 record 的 id

  const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: remarkFormSchema,
    showActionButtonGroup: false,
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    recordId.value = data.record.id; // 存储 record 的 id
    setFieldsValue({
      owner: data.record.owner || '',
    });
  });

  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      values.id = recordId.value; // 手动添加 id 到 values 对象中
      emit('success', values); // 传递包含 id 的 values
      closeModal();
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
